package com.yunyao.web.controller;

/**
 * 收款管理
 *
 * @author chenyejian
 * @Date 2024/7/31 11:13
 */

import com.yunyao.common.dto.payment.PaymentDto;
import com.yunyao.common.web.util.PageResult;
import com.yunyao.common.web.util.StringUtils;
import com.yunyao.common.web.util.YResult;
import com.yunyao.framework.dto.audit.ApprovalDto;
import com.yunyao.framework.dto.audit.SubmitApprovalDto;
import com.yunyao.framework.dto.payment.ReceiveNodeSaveDto;
import com.yunyao.framework.dto.payment.ReceiveSaveDto;
import com.yunyao.framework.service.ReceivePaymentService;
import com.yunyao.framework.vo.pay.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/receivePayment")
public class ReceivePaymentController {
    @Autowired
    private ReceivePaymentService receivePaymentService;

    @GetMapping("/list")
    public YResult<PageResult<ReceivePaymentVO>> list(PaymentDto paymentDto) {
        if (!StringUtils.isEmpty(paymentDto.getBeginDate())) {
            paymentDto.setBeginDate(paymentDto.getBeginDate() + " 00:00:00");
        }
        if (!StringUtils.isEmpty(paymentDto.getEndDate())) {
            paymentDto.setEndDate(paymentDto.getEndDate() + " 23:59:59");
        }
        return receivePaymentService.pageList(paymentDto);
    }

    /**
     * 统计应收未收，逾期金额
     * @param paymentDto
     * @return
     */
    @GetMapping("/statistical/price")
    public YResult<ReceiveStatisticalPriceVO> nodeList(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getReceiveId()) && StringUtils.isEmpty(paymentDto.getContractId())) {
            return YResult.error("ReceiveId不能为空或ContractId不能为空");
        }
        return receivePaymentService.getStatisticalPrice(paymentDto);
    }


    @GetMapping("/info")
    public YResult<ReceivePaymentVO> info(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getId())) {
            return YResult.error("id不能为空");
        }
        return receivePaymentService.getDetailById(paymentDto.getId());
    }


    /**
     * 删除收款信息
     */
    @PostMapping("/deleteById")
    public YResult<String> deleteById(@RequestBody PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getId())) {
            return YResult.error("id不能为空");
        }
        return receivePaymentService.deleteById(paymentDto.getId());
    }


    /**
     * 新增、编辑
     */
    @PostMapping("/saveOrUpdate")
    public YResult<String> saveOrUpdate(@RequestBody ReceiveSaveDto dto) {
        if (null == dto) {
            return YResult.error("不能为空");
        }
        if (StringUtils.isEmpty(dto.getContractId())) {
            return YResult.error("ContractId不能为空");
        }
        return receivePaymentService.saveOrUpdate(dto);
    }


    /**
     * 新增、编辑
     */
    @PostMapping("/node/saveOrUpdate")
    public YResult<String> saveOrUpdateNode(@RequestBody ReceiveNodeSaveDto dto) {
        if (null == dto) {
            return YResult.error("不能为空");
        }
        return receivePaymentService.nodeSaveOrUpdate(dto);
    }


    /**
     * 合同收款历史
     */
    @GetMapping("/history")
    public YResult<List<ReceiveHistoryVO>> history(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getReceiveId())&&StringUtils.isEmpty(paymentDto.getContractId())) {
            return YResult.error("receiveId或contractId不能为空");
        }
        return receivePaymentService.getHistoryList(paymentDto);
    }


    /**
     * 合同收款发票，新增获取
     */
    @GetMapping("/invoice")
    public YResult<List<ReceiveContractInvoiceVO>> invoice(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getContractId())) {
            return YResult.error("contractId不能为空");
        }
        return receivePaymentService.getNowInvoiceList(paymentDto.getContractId());
    }

    /**
     * 合同收款发票，查看、编辑获取
     */
    @GetMapping("/getReceiveInvoice")
    public YResult<List<ReceiveContractInvoiceVO>> getReceiveInvoice(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getReceiveId())) {
            return YResult.error("ReceiveId不能为空");
        }
        return receivePaymentService.getReceiveInvoiceList(paymentDto);
    }


    /**
     * 合同发票关联收款发票
     */
    @PostMapping("/invoiceConnect")
    public YResult<String> invoiceConnect(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getInvoiceId())) {
            return YResult.error("InvoiceId不能为空");
        }
        if (StringUtils.isEmpty(paymentDto.getReceiveId())) {
            return YResult.error("ReceiveId不能为空");
        }
        return receivePaymentService.invoiceConnect(paymentDto);
    }


    /**
     * 合同收款节点
     */
    @GetMapping("/node")
    public YResult<List<ReceiveNodeVO>> node(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getId())) {
            return YResult.error("receiveId不能为空");
        }
        return receivePaymentService.getReceiveNodeList(paymentDto);
    }


    /**
     * 提交审批收款管理
     */
    @PostMapping("/submit/approval")
    public YResult<String> submitApproval(@RequestBody @Validated SubmitApprovalDto submitApprovalDto) {
        return receivePaymentService.submitApproval(submitApprovalDto);
    }


    /**
     * 审批收款管理
     *
     * @return
     */
    @PostMapping("/approval")
    public YResult<String> approval(@RequestBody @Validated ApprovalDto approvalDto) {
        return receivePaymentService.approval(approvalDto);
    }

}
